package Collections;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class MonitorVehicleTracker2 {

		
		private final ConcurrentMap<String, ImutablePoint> locations;
		private final Map<String, ImutablePoint> unmodifiableMap;
		
		public MonitorVehicleTracker2(Map<String, ImutablePoint> data) {
			locations = new ConcurrentHashMap<String, ImutablePoint>(data);
			unmodifiableMap = Collections.unmodifiableMap(locations);
			
		}

		public ImutablePoint getLocation(String name) {
			
			ImutablePoint loc = safeGet(name);
			
			return loc; //create a copy to forbid changes in state loc
		}
		
		public void setLocation (String name, ImutablePoint point) {			
			
			if(locations.replace(name, point) == null)
				throw new IllegalArgumentException("invalid vehicle name: " + name);
		}

		private ImutablePoint safeGet(String name) {
			ImutablePoint loc = locations.get(name);
			if (loc == null) {
				throw new IllegalArgumentException("no such ID: " + name);
			}
			return loc;
		}
		
		public Map<String, ImutablePoint> getLocations() {
			return unmodifiableMap;
		}

}
